#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <sys/time.h>
#include <stddef.h>
#include <iostream>

#include "Logger.hpp"
#include "CsvReader.hpp"
#include "CsvWriter.hpp"
#include "Sorter.hpp"

int main(int argc, char** argv) {
	if (argc != 3) {
		std::cout << "Usage: ./Sort <input-file> <output-file>\n";
		return 0;
	}
	
	Logger::getInstance().setFile("log");

	struct timeval start, end;
	gettimeofday(&start, NULL);

	CsvReader reader = CsvReader(argv[1]);
	reader.read();
	int nChunks = reader.getNChunks();
	Sorter sorter = Sorter(nChunks);
	sorter.sortAndMerge();
	CsvWriter writer = CsvWriter(argv[2]);
	if (1 == nChunks) {
		char buf[] = "tmp1";
		writer.setFile(buf);
	} else {
		char buf[] = "tmpRes";
		writer.setFile(buf);
	}
	writer.writeResult();

	gettimeofday(&end, NULL);	
	Logger::getInstance().log(Logger::INFO, "Ready. Time elapsed : %d seconds.\n", (end.tv_sec - start.tv_sec));
	return 0;
}


